
Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    circular: true,
    useData:""
  },
  onLoad: function(options) {
    //页面初始化 options为页面跳转所带来的参数
    //this.setData可以让view重绘
    this.onQuery();
    this.onQueryNews();
  },
  onQuery: function(openid){
    let that = this;

    const db = wx.cloud.database()
    db.collection('articles').get().then(res => {
      console.log('[数据库] [查询记录] 成功: ', res)
      let articles = res.data;
      let arr = [];
      articles.forEach((element,idx) => {
        element.index = idx;
        arr.push(element);
      });
      that.setData({
        articles: arr
      });

    })
  },

  onQueryNews: function(openid){
    let that = this;

    const db = wx.cloud.database()
    db.collection('news').get().then(res => {
      console.log('[数据库] [查询记录] 成功: ', res)
      let articles = res.data;
      let arr = [];
      articles.forEach((element,idx) => {
        element.index = idx;
        arr.push(element);
      });
      that.setData({
        news: arr
      });

    })
  },
  goArticle:function(e){
    let id = e.currentTarget.dataset.id;

    wx.navigateTo({
      url: '../article/article?id='+id
    })
  },
  //跳转详情页
  goNewsDetail:function(e){
    let id = e.currentTarget.dataset.id;

    wx.navigateTo({
      url: '../news/news?id='+id
    })
  }
})